package core

import (
	"io/ioutil"
	"strings"

	"github.com/go-ini/ini"
	"gopkg.in/yaml.v2"
)

type Conf struct {
	NotePath string
	OutPath  string
}

type ConfigYaml struct {
	NotePath    string     `yaml:"notePath"`
	WebPath     string     `yaml:"webPath"`
	Lang        string     `yaml:"lang"`
	Ico         string     `yaml:"ico"`
	Title       string     `yaml:"title"`
	Keywords    []string   `yaml:"keywords"`
	Description string     `yaml:"description"`
	Style       []string   `yaml:"style"`
	Script      []string   `yaml:"script"`
	Copyritght  string     `yaml:"copyritght"`
	Top         [][]string `yaml:"top"`
}

func getConf() {
	cfg := &ConfigYaml{}
	con, err := ioutil.ReadFile("./config.yaml")
	if err == nil {
		err := yaml.NewDecoder(strings.NewReader(string(con))).Decode(cfg)
		if err != nil {
			log(err.Error())
		}
		log(cfg)
	} else {
		log(err.Error())
	}
}
func _loadConfig() Conf {
	conf := Conf{}
	cfg, err := ini.Load("config.ini")
	if err != nil {
		log("Fail to load config.ini" + err.Error())
	}
	note := func(key string) *ini.Key {
		return cfg.Section("note").Key(key)
	}
	conf.NotePath = strings.TrimSpace(note("notePath").String())
	conf.OutPath = strings.TrimSpace(note("outPath").String())
	return conf
}

func InitConfig() Conf {
	return _loadConfig()
}

func getWebSiteByKey(key string) string {
	cfg, err := ini.Load("config.ini")
	if err != nil {
		log("Fail to load config.ini" + err.Error())
	}
	website := func(key string) *ini.Key {
		return cfg.Section("website").Key(key)
	}
	return strings.TrimSpace(website(key).String())
}
func getNavByKey(key string) string {
	cfg, err := ini.Load("config.ini")
	if err != nil {
		log("Fail to load config.ini" + err.Error())
	}
	website := func(key string) *ini.Key {
		return cfg.Section("navigation").Key(key)
	}
	return strings.TrimSpace(website(key).String())
}
